// special graphd instance for internal communication among nodes
// 
// masterd waits for N nodes to register, and then publishes config info
package main

import (
	graph "code.google.com/p/feedgraph/graph/httpd"
	"code.google.com/p/feedgraph/config"
	"net/http"
	"flag"
	"log"
	"time"
)

const addr = ":81"


func main() {

	n := flag.Int("n", 1, "number of slave nodes to wait for")

	flag.Parse()

	graph.AddGraph("FEEDGRAPH", 8)

	// wait for n slaves
	go func () {

		g := graph.Graph("FEEDGRAPH")

		slaves := g.EdgesExactly("SLAVES", "registered", *n)

		log.Println("masterd ready with ", *n, "slaves")

		for _, e := range slaves {

			addr := e.GetString("Addr")

			log.Println("slave at", addr)

			config.Local.ClusterConfig.Nodes = append(config.Local.ClusterConfig.Nodes, config.Node{addr})
		}

		g.SetNode("CONFIG", config.Local)

		g.Link("MASTER", "ready", struct{}{}, time.Now())
	} ()

	http.ListenAndServe(addr, nil)
}
